package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Clazz;
import org.example.pojo.ClazzQueryParam;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    /*
    * 分页查询
    * */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询参数：{}，{}，{}，{}，{}", clazzQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /*
    * 新增班级
    * */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("新增班级数据：{}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /*
    * 删除班级
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级数据：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /*
    * 根据ID查询
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询班级数据：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /*
    * 修改班级信息
    * */
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级数据：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /*
    * 查询所有班级信息
    * */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有班级信息");
        List<Clazz> clazzList = clazzService.All();
        return Result.success(clazzList);
    }
}
